package com.ahy231.controller;

import com.ahy231.dto.Result;
import com.ahy231.dto.ResultCode;
import com.ahy231.dto.UserInfo;
import com.ahy231.service.UserService;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.*;

/**
 * @author ahy231
 * @date 2021/7/23 8:59
 * @description 用户管理
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }

    @ApiOperation("微信登录")
    @PostMapping(value = "/wxLogin")
    public Result wxLogin(@ApiParam("密文") @RequestParam String encryptedData, @ApiParam("偏移量") @RequestParam String iv, @ApiParam("登录凭证") @RequestParam String code) {
        try {
            String data = userService.wxLogin(encryptedData, iv, code);
//            log.error(data);
            return new Result(ResultCode.SUCCESS, data);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("添加用户角色（driver, worksite, company）")
    @PutMapping("/addRole")
    @RequiresRoles("user")
    public Result addRole(@ApiParam(value = "role", allowableValues = "driver,worksite,company", required = true) @RequestParam String role) {
        try {
            return new Result(ResultCode.SUCCESS, userService.addRole(role));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("移除用户角色（driver, worksite, company）")
    @DeleteMapping("/deleteRole")
    @RequiresRoles("user")
    public Result removeRole(@ApiParam(value = "role", allowableValues = "driver,worksite,company", required = true) @RequestParam String role) {
        try {
            return new Result(ResultCode.SUCCESS, userService.removeRole(role));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("修改用户信息（不修改部分不用填）")
    @PostMapping("/modify")
    @RequiresRoles("user")
    public Result modifyUserInfo(@ApiParam("用户信息") @RequestBody UserInfo userInfo) {
        try {
            return new Result(ResultCode.SUCCESS, userService.modifyUserInfo(userInfo));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查看用户信息")
    @PostMapping("/get")
    @RequiresRoles("user")
    public Result getUserInfo(@ApiParam("用户id") @RequestParam(required = false) Long userId) {
        try {
            return new Result(ResultCode.SUCCESS, userService.getUserInfo(userId));
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("查看用户角色")
    @PostMapping("/roles")
    @RequiresRoles("user")
    public Result getRoles() {
        try {
            return new Result(ResultCode.SUCCESS, userService.getRoles());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }

    @ApiOperation("测试登录")
    @PostMapping("/testLogin")
    public Result testLogin() {
        try {
            return new Result(ResultCode.SUCCESS, userService.testLogin());
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(ResultCode.FAILED, e.getMessage());
        }
    }
}
